
module C2 where

import Types
import Network.URI
import Network.HTTP.Base
import qualified Data.Map as Map

baseUri x = URI "http:" (Just (URIAuth "" "www.collective2.com" "")) "/cgi-bin/signal.mpl?" x ""

getUri xs = baseUri (urlEncodeVars xs)

data C2Action = BTO | SSHORT | STO | BTC | STC
        deriving (Show)
        
data C2Duration = GTC | DAY
        deriving (Show)

data C2Instrument = Stock | Option | Future | Forex

instance Show C2Instrument where
        show Stock = "stock"
        show Option = "option"
        show Future = "future"
        show Forex = "forex"

type C2Request = Map.Map String String

getC2Uri r = getUri (Map.assocs r)

listSystemPwd s p = [("systemId", s), ("pw", p)]

makeSignalCmd systemId pwd = Map.fromList $ ("cmd", "signal") : (listSystemPwd systemId pwd)
makeFlushCmd systemId pwd  = Map.fromList $ ("cmd", "flushpendingsignals") : (listSystemPwd systemId pwd)
makeCloseAll systemId pwd  = Map.fromList $ ("cmd", "closeallpositions") : (listSystemPwd systemId pwd)

addConditional r id = Map.insert "conditionalupon" (show id) r

addStopLoss r (Just sl)    = Map.insert "stoploss" (show sl) r
addStopLoss r Nothing      = r

addTakeProfit r (Just tp)  = Map.insert "profittarget" (show tp) r
addTakeProfit r Nothing    = r

addParkUntil r secs = Map.insert "parkuntil" (show secs) r
addAction r a       = Map.insert "action" (show a) r

addLimitPrice r (Just p)   = Map.insert "limit" (show p) r
addLimitPrice r Nothing    = r

addStopPrice r (Just p)    = Map.insert "stop" (show p) r
addStopPrice r Nothing     = r

addSymbol :: C2Request->Symbol->C2Request
addSymbol r symbol  = Map.insert "symbol" symbol r

addInstrument :: C2Request->C2Instrument->C2Request
addInstrument r instr = Map.insert "instrument" (show instr) r

addQuant :: C2Request->Int->C2Request
addQuant r q        = Map.insert "quant" (show q) r


getOcaGroupIdRequest = getUri [("cmd", "requestocaid")]

